﻿Imports System.Windows.Forms

Public Class LockRulesDialog

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        If IDBox.Text = "" Then
            MessageBox.Show("You need to enter an ID for this rule", "No ID entered", MessageBoxButtons.OK, MessageBoxIcon.Error)
            IDBox.Focus()
            Exit Sub
        End If

        If DescriptionBox.Text = "" Then
            MessageBox.Show("You need to enter a description for this rule", "No description entered", MessageBoxButtons.OK, MessageBoxIcon.Error)
            DescriptionBox.Focus()
            Exit Sub
        End If

        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub


    Public Sub LoadPeriodBox()
        Dim sql As String

        sql = "Select * from Periods"

        PeriodBox.DataSource = LoadDataView(sql)
        PeriodBox.ValueMember = "Period_ID"
        PeriodBox.DisplayMember = "Period_Name"

        PeriodBox.SelectedValue = CurrentPeriodID

    End Sub

    Public Sub LoadRegionBox()
        Dim sql As String

        sql = "select Region_Code, Region_Name from Regions union Select '<All>', '<All>' from global_settings_Upgrade"

        RegionBox.DataSource = LoadDataView(sql)
        RegionBox.ValueMember = "Region_Code"
        RegionBox.DisplayMember = "Region_Name"
    End Sub

    Public Sub LoadLABox()
        Dim sql As String

        If RegionBox.SelectedValue.ToString = "<All>" Then
            sql = "select LA_Code, LA_Name from Local_Authorities Union select '<All>', '<All>' from global_settings_Upgrade"
        Else
            sql = "select LA_Code, LA_Name from Local_Authorities where Region_Code = '" & _
                  RegionBox.SelectedValue.ToString & "' Union select '<All>', '<All>' from global_settings_upgrade"
        End If

        LABox.DataSource = LoadDataView(sql)
        LABox.ValueMember = "LA_Code"
        LABox.DisplayMember = "LA_Name"

    End Sub

    Public Sub LoadProviderBox()
        Dim sql As String

        If LABox.SelectedValue.ToString = "<All>" And RegionBox.SelectedValue.ToString = "<All>" Then
            sql = "select UPIN, Provider_Name from Providers union select 0, '<All>' from global_settings_Upgrade"
        Else
            If LABox.SelectedValue.ToString = "<All>" Then
                sql = "select p.UPIN, p.Provider_Name " & _
                      "from providers p, relationships r, Local_Authorities la " & _
                      "where p.UPIN = r.UPIN " & _
                      "and r.LA_Code = la.LA_Code " & _
                      "and la.Region_Code = '" & RegionBox.SelectedValue.ToString & "' " & _
                      "union select 0, '<All>' from global_settings_upgrade"
            Else
                sql = "select p.UPIN, p.Provider_Name " & _
                      "from providers p, relationships r, Local_Authorities la " & _
                      "where p.UPIN = r.UPIN " & _
                      "and r.LA_Code = la.LA_Code " & _
                      "and la.LA_Code = '" & LABox.SelectedValue.ToString & "' " & _
                      "union select 0, '<All>' from global_settings_upgrade"
            End If
        End If

        ProviderBox.DataSource = LoadDataView(sql)
        ProviderBox.ValueMember = "UPIN"
        ProviderBox.DisplayMember = "Provider_Name"

    End Sub

    Public Sub LoadFieldBox()
        Dim sql As String

        sql = "select Field_Code, Field_Description, order_Field from Fields_List_Part " & _
              "union select 0, '<All>', 0 from global_settings_upgrade " & _
              "order by Order_Field"

        FieldBox.DataSource = LoadDataView(sql)
        FieldBox.ValueMember = "Field_Code"
        FieldBox.DisplayMember = "Field_Description"
    End Sub

    Private Sub DatesCheckBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DatesCheckBox.CheckedChanged
        If DatesCheckBox.Checked = True Then
            DateGroup.Enabled = True

        Else
            DateGroup.Enabled = False
        End If
    End Sub

    Private Sub RegionBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RegionBox.SelectedIndexChanged
        LoadLABox()
        LoadProviderBox()

    End Sub

    Private Sub LABox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LABox.SelectedIndexChanged
        LoadProviderBox()

    End Sub

    Private Sub FromBox_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FromBox.ValueChanged
        If FromBox.Value.Date > ToBox.Value.Date Then
            ToBox.Value = FromBox.Value
        End If
    End Sub

    Private Sub ToBox_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToBox.ValueChanged
        If FromBox.Value.Date > ToBox.Value.Date Then
            FromBox.Value = ToBox.Value
        End If
    End Sub
End Class
